package com.junzhiit.nengji.app.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.app.service.IndustryService;
import com.junzhiit.nengji.app.web.util.Constants;
import com.junzhiit.nengji.app.web.vo.IndustryVO;
import com.junzhiit.nengji.app.web.vo.ResultVO;
import com.junzhiit.nengji.dao.IndustryDAO;
import com.junzhiit.nengji.model.Industry;

@Service
public class IndustryServiceImpl extends BaseServiceImpl implements IndustryService {

	@Autowired
	private IndustryDAO industryDAO;
	
	
	@Override
	public ResultVO getIndustryList(String parentId) {
		ResultVO result = new ResultVO();
		List<Industry> typeList = industryDAO.findAll(parentId);
		List<IndustryVO> list = treeList(typeList, Constants.ROOT_VALUE);
		result.setData(list);
		return result;
	}

	public List<IndustryVO> treeList(List<Industry> typeList, String parentId) {  
		List<IndustryVO> childTypeList = new ArrayList<IndustryVO>();
		for (Industry industry : typeList) {
			IndustryVO industryVO = new IndustryVO();
			BeanUtils.copyProperties(industry, industryVO);
			String id = industry.getId();
			String pid = industry.getParentId();
			if (parentId.equals(pid)) {
				List<IndustryVO> childList = treeList(typeList, id);
				industryVO.setChildren(childList);
				industryVO.setChildCount(childList.size());
				childTypeList.add(industryVO);
			}
		}
		return childTypeList;  
	}

	@Override
	public ResultVO getIndustryFullName(String parentId) {
		ResultVO result = new ResultVO();
		result.setData(industryDAO.getIndustryFullName(parentId));
		return result;
	}

	@Override
	public ResultVO getAll() {
		ResultVO result=new ResultVO();
		List<Industry> list=industryDAO.findAll(null);
		result.setData(list);
		return result;
	}

}
